package demo;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import bean.Order;
import bean.Person;

public class Demo1 {
	
	
	
	 SqlSessionFactory factory;
		public  void before() throws IOException{
			 InputStream is = Resources.getResourceAsStream("sqlMapconfig.xml");
			 factory = new SqlSessionFactoryBuilder().build(is);
		}
	public void demo(){
		SqlSession session = factory.openSession();
		Map<String,Object> map=new HashMap<String,Object>();
		//map.put("name","毛毛");
		map.put("age",19 );
		List<Person> list = session.selectList("com.personMaper1.select1",map);
		for(Person l:list){
		System.out.println(l);}
		session.close();
	}
  public static void main(String[] args) throws IOException {
		Demo1 d=new Demo1();
	    d.before();
	    d.demo7();
	    
	}

  
  
  public void demo1(){
		SqlSession session = factory.openSession();
		Map<String,Object> map=new HashMap<String,Object>();
		//map.put("name","毛毛");
		map.put("age",44 );
		map.put("id",2);
		int i = session.update("com.personMaper1.update",map);	
		System.out.println(i);
		session.commit();
		session.rollback();
		session.close();
	}
  
  
  
  public void demo2(){
		SqlSession session = factory.openSession();
		Map<String,Object> map=new HashMap<String,Object>();
		//map.put("name","毛毛");
		List<Integer> list=new ArrayList<Integer>();
		//list.add(1);
		list.add(2);
		list.add(3);
		list.add(4);
		
		map.put("nn",list);
	    List<Person> list1 = session.selectList("com.personMaper1.selectin",map);	
		for(Person i:list1){
	    System.out.println(i);}
		/*session.commit();*/
		session.close();
	}
  
  
  
  public void demo3(){
		SqlSession session = factory.openSession();
		Map<String,Object> map=new HashMap<String,Object>();
		try{
		List<Person> list=new ArrayList<Person>();
		for(int i=10;i<506;i++)
		{
			Person p=new Person();
			p.setName("没事"+i);
			p.setAge(i-5);
			list.add(p);
			if(i%100==0)
			{
			
			map.put("persons",list);
			
			 session.insert("com.personMaper1.insertid",map);	
			 list.clear();
			
			}
		}
		
		map.put("persons",list);
		
		 session.insert("com.personMaper1.insertid",map);	
	
		session.commit();
		}catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		}finally{
		
		
		session.close();}
	}
  
  
  
  public void demo4(){
		SqlSession session = factory.openSession();
		Map<String,Object> map=new HashMap<String,Object>();
		try{
		List<Integer> list=new ArrayList<Integer>();
		for(int i=1050;i<1060;i++)
		{
			
		    
		    list.add(i);
			if(i%100==0)
			{
			map.put("ids",list);
			 session.insert("com.personMaper1.deletdid",map);	
		
			
			}
		}
		
		map.put("ids",list);
		
		 session.insert("com.personMaper1.deletdid",map);	
	
		session.commit();
		}catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		}finally{
		
		
		session.close();}
	}
  
  
  public void demo5(){
		SqlSession session = factory.openSession();
		
		Person p = session.selectOne("com.personMaper1.selectByorder",5);	
		List<Order> list = p.getList();
		for(Order l:list)
		{
			System.out.println(l);
		}
		session.commit();
		session.rollback();
		session.close();
	}

  
  
  public void demo6(){
		SqlSession session = factory.openSession();
		
		Order o = session.selectOne("com.orderMaper.selectManytoone",2);	
			System.out.println(o);
		
		session.commit();
		session.rollback();
		session.close();
	}
  
  
  public void demo7(){
		SqlSession session = factory.openSession();
		
		List<Person> o = session.selectList("com.personMaper1.ManyTOMany",2);	
			
		for(Person p:o){
		System.out.println(p);}
		
		session.commit();
		session.rollback();
		session.close();
	}

}
